package core

import (
	"encoding/json"
	"io/ioutil"
	"os"
)

// 创建文件夹
func MkDirAll(dirPath string) error {
	err := os.MkdirAll(dirPath, 0664)
	return err
}

// 解析JSON文件
func DecodeJsonFile(jsonPath string, v interface{}) error {
	bts, err := ioutil.ReadFile(jsonPath)
	if err != nil {
		return err
	}
	err = json.Unmarshal(bts, v)
	return err
}

// 记录日志
func WriteLogs(logPath string, content []string, isAppend bool) (err error) {
	var f *os.File
	if isAppend {
		f, err = os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664)
	} else {
		f, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0664)
	}
	if err != nil {
		return err
	}
	var vs string
	for _, v := range content {
		vs += v + "\n"
	}
	_, err = f.WriteString(vs)
	return err
}

// 文件保存
func SaveFile(filePath string, data []byte) error {
	err := ioutil.WriteFile(filePath, data, 0664)
	return err
}
